<?php
class SiteAdmin_CountryController extends SiteAdminController
{
		
   	public function indexAction() {
    	$iPage = intval($this->getRequest()->getParam('page', 1));
    	$this->view->countries = Obj_Country::getCountries();  	
    }
    
   	public function editAction() {
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$addnew = intval($this->getRequest()->getParam('addnew', 0)) === 1 ? true : false;
    	$arCountry = Obj_Country::getCountry($id);
    	if ($addnew || $arCountry === false) {
    		$this->view->country = array(
    			'ID' => 0,
    			'Name' => '',
    			'alias' => '',
    			'description' => '',
    			'active' => 0,
    			'sort' => 0,
    			'seoTitle' => '',
    			'seoDescription' => '',
    			'seoKeywords' => '',
    		);
    	}
    	else {
    		$this->view->country = $arCountry;
    	}
    }
    
    public function saveAction() {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
		$arCountry = array();
    	$id = (int) $this->getRequest()->getParam('ID', 0);
    	$arCountry['Name'] = trim($this->getRequest()->getParam('Name', ''));
    	if ($arCountry['Name'] == '') {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Страна должна иметь название!')), true, false);
    	}
    	$arCountry['alias'] = Obj_Text::translit($arCountry['Name']);
    	$arCountry['sort'] = intval($this->getRequest()->getParam('sort', 0));
    	if ($arCountry['alias'] == '') {
    		$this->_helper->json(array('error' => 1, 'errorStr' => _('Страна должна иметь алиас!')), true, false);
    	}
    	$arCountry['description'] = trim($this->getRequest()->getParam('description', ''));
    	$arCountry['seoTitle'] = trim($this->getRequest()->getParam('seoTitle', ''));
    	$arCountry['seoDescription'] = trim($this->getRequest()->getParam('seoDescription', ''));
    	$arCountry['seoKeywords'] = trim($this->getRequest()->getParam('seoKeywords', ''));
    	$arCountry['pageText'] = trim(Obj_Text::removeHTMLComments($this->getRequest()->getParam('pageText', '')));
    	$arCountry['active'] = intval($this->getRequest()->getParam('active', 0));
    	$cid = Obj_Country::saveCountry($arCountry, $id);
    	if ($arCountry['active'] === 1){
    		Obj_System_SearchIndex::write($cid, CONTENT_TYPE_COUNTRY, $arCountry['Name'], $arCountry['description']);
    	}
    	else Obj_System_SearchIndex::delete($cid, CONTENT_TYPE_COUNTRY);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index'))), true, false);
    }

    public function deleteAction() {
        if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	Obj_Country::delCountry($id);
    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_COUNTRY);
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index'))), true, false);  
    }
    
    public function stateChangerAction() {
		if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = (int) $this->getRequest()->getParam('id', 0);
    	$arCountry = Obj_Country::getCountry($id);
    	if(intval($arCountry['active']) === 1){
    		$arCountry['active'] = 0;
    		$cid = Obj_Country::saveCountry($arCountry, $id);
    		Obj_System_SearchIndex::delete($id, CONTENT_TYPE_COUNTRY);
    		
    	} else {
    		$arCountry['active'] = 1;
    		$cid = Obj_Country::saveCountry($arCountry, $id);
    		Obj_System_SearchIndex::write($id, CONTENT_TYPE_COUNTRY, $arCountry['Name'], $arCountry['description']);
    	}
    	$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index'))), true, false);
    }

        
    public function groupedDeleteAction(){
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$arDelete = array();
    	$arDelete = $this->getRequest()->getParam('arDelete', null);
    	foreach ($arDelete as $k => $v){
	    	$id = (int) $v;
	    	Obj_Country::delCountry($id);
	    	Obj_System_SearchIndex::delete($id, CONTENT_TYPE_COUNTRY);
    	}
		$this->_helper->json(array('error' => 0, 'redirect' => $this->view->url(array('action' => 'index'))), true, false);
    }
    
}